﻿/***************************************************************************
 //* Foxit Software Inc.
 //* Copyright(C) 2005-2012, all rights reserved.
 //*
 //* The following code is copyrighted and contains proprietary information
 //* and trade secrets of Foxit Software Inc.                                                      *
 ***************************************************************************/

/**
 * 缩略图显示
 */
define(function (require, exports, module) {
    WebPDF.PDFView.STANDARD_THUMB_WIDTH = 128;
    WebPDF.PDFView.STANDARD_THUMB_HEIGHT = 149;
    WebPDF.PDFView.STANDARD_THUMB_CONTAINER_WIDTH = 138;
    WebPDF.PDFView.STANDARD_THUMB_CONTAINER_HEIGHT = 179;
    WebPDF.PDFView.STANDARD_THUMB_WIDTH_MOBILE = 172;
    WebPDF.PDFView.STANDARD_THUMB_HEIGHT_MOBILE = 244;
    var Config = WebPDF.Config;
    WebPDF.PDFView.CPDF_PageThumb = function (pdfPage, thumbsView) {
        if (!pdfPage)
            $.error("Parameter 'pdfPage' cannot be null while create thumb.");
        var _pdfPage = pdfPage,
            _thumbImgOffsetTop = 0,
            _thumbWidth = 0, 
            _thumbHeight = 0, 
            _isContentCreated = false,
            _thumbsView = thumbsView,
            _pageIndex = _pdfPage.getPageIndex(),
            _thumbContainerID = _thumbsView.getPageViewContainerID(_pageIndex),
            _thumbBackgroundImageID = _thumbsView.getPageBackgroundImgID(_pageIndex),
            _wrapThumbID = _thumbsView.getWrapPageID(_pageIndex),
            _thumbLoaderImageID = _thumbsView.getPageLoaderImageID(_pageIndex),
            _thumbPageNumberID = _thumbsView.getThumbPageNumberID(_pageIndex),
            //页面大小是否更新过,当改变放大倍数时重置;
            _isDimensionUpdated = false,
            //页面是否已经Load完成;
            _isThumbLoaded = false,
            _isLoadError = false;
        
        this.getPageIndex = function () {
            return pdfPage.getPageIndex();
        };
        this.getPageViewContainerID = function () {
            return _thumbContainerID;
        };
        this.setThumbImgOffsetTop = function (offsetTop) {
            _thumbImgOffsetTop = offsetTop;
        };
        this.getThumbImgOffsetTop = function () {
            return _thumbImgOffsetTop;
        };
        this.getPageBackgroundImgID = function () {
            return _thumbBackgroundImageID;
        };
        this.getWrapPageID = function () {
            return _wrapThumbID;
        };
        this.getPageLoaderImageID = function () {
            return _thumbLoaderImageID;
        };
        this.getThumbPageNumberID = function () {
            return _thumbPageNumberID;
        };

        this.getPageViewWidth = function () {
            return _thumbWidth;
        };
        this.setPageViewWidth = function (width) {
            if (isNaN(width))
                return;
            _thumbWidth = width;
        };
        this.getDocView = function () {
            return _thumbsView;
        };
        this.getPageViewHeight = function () {
            return _thumbHeight;
        };
        this.setPageViewHeight = function (height) {
            if (isNaN(height))
                return;
            _thumbHeight = height;
        };
        this.getPDFPage = function () {
            return _pdfPage;
        };
        this.isContentCreated = function () {
            return _isContentCreated;
        };
        //设置page对应的div是否创建的状态;
        this.setContentCreatedState = function (isCreated) {
            _isContentCreated = isCreated;
        };
        this.isDimensionUpdated = function () {
            return _isDimensionUpdated;
        };
        this.setDimensionUpdated = function (isUpdated) {
            _isDimensionUpdated = isUpdated;
        };
        this.updateDimension = function () {
        };
        this.isThumb = function () {
            return true;
        };
        this.getScale = function () {
            return 1;
        };
        this.setScale = function(){
        };

        //显示页面;
        this.show = function () {
            $("#" + _wrapThumbID).addClass("fwr-hidden");
            $("#" + _thumbBackgroundImageID).removeClass("fwr-hidden");
            $("#" + _thumbLoaderImageID).hide();
        };

        this.showErrorPage = function (failedImageUrl) {
            var jqWrap = $("#" + _wrapThumbID);
            jqWrap.html('');
            jqWrap.removeClass("fwr-hidden");
            $("#" + _thumbBackgroundImageID).addClass("fwr-hidden");
            $("#" + _thumbLoaderImageID).hide();

            var thumbWidth = jqWrap.width();
            var thumbHeight = jqWrap.height();

            var fZoomX = Config.defaults.errPageImgWidth / thumbWidth;
            var fZoomY = Config.defaults.errPageImgHeight / thumbHeight;
            var fZoom = fZoomX >= fZoomY ? fZoomX : fZoomY;
            fZoom = fZoom > 1 ? fZoom : 1;
            var fRealWidth = Config.defaults.errPageImgWidth / fZoom;
            var fRealHeight = Config.defaults.errPageImgHeight / fZoom;
            var marginLeft = (thumbWidth - fRealWidth) / 2;
            var marginTop = (thumbHeight - fRealHeight) / 2;

            var html = "<div style='position:absolute;width:" + fRealWidth + "px;height:" + fRealHeight
                + "px;margin-left:" + marginLeft + "px;margin-top:" + marginTop + "px'><img alt='' class='thumbimage' src='"
                + failedImageUrl + "'  style='width:100%;height:100%' page-index='" + _pdfPage.getPageIndex() + "'></div>";
            jqWrap.append(html);
        };

        this.isPageLoaded = function () {
            return _isThumbLoaded;
        };
        this.setPageLoaded = function (isLoaded) {
            _isThumbLoaded = isLoaded;
        };
        this.isPageLoadError = function () {
            return _isLoadError;
        };
        this.setPageLoadError = function (isLoadError) {
        	_isLoadError = isLoadError;
        };
    };
    return WebPDF.PDFView.CPDF_PageThumb;
});